/* ====================================================
#   Copyright (C)2019 All rights reserved.
#
#   Author        : your name
#   Email         : your email
#   File Name     : numberconvert.c
#   Last Modified : 2019-10-09 15:43
#   Describe      :
#
# ====================================================*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

bool issHexNumber(const char* pBuf, int bufSize)
{
    unsigned int i = 0;

    if ((NULL == pBuf) || (0 == bufSize))
    {
        return false;
    }

    for (i = 0; i < bufSize; ++i)
    {
        if ( (pBuf[i] == 'x' || pBuf[i] == 'X') 
                || ( ((pBuf[i] > '9'))
                    && ( (pBuf[i] > 'A'&& pBuf[i] < 'F')
                        ||(pBuf[i] > 'a' && pBuf[i] < 'f') ) ))
        {
            return true;
        }
    }
    return false;
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        printf("usage: %s <number>\n",argv[0]);
        return -1;
    }

    long number = 0;
    if (issHexNumber(argv[1],strlen(argv[1])+1))
    {
        number = strtol(argv[1],NULL,16);
    } else 
    {
        number = strtol(argv[1],NULL,10);
    }

    printf("%s = (D) %li\n",argv[1],number);
    printf("%s = (H) 0x%lx\n",argv[1],number);
    return 0;
}

